﻿using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Autodesk.Revit.Attributes;
using YJKCodeBase.WinForm;

namespace YJKTextDimension.Utility
{
	[Regeneration(RegenerationOption.Manual)]
	[Journaling(JournalingMode.NoCommandData)]
	[Transaction(TransactionMode.Manual)]
	public class WindowHelper
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public static WindowHandle HWndRevit
		{
			get
			{
				WindowHelper.NewRevitWnd();
				return WindowHelper._hWndRevit;
			}
			set
			{
				WindowHelper._hWndRevit = value;
			}
		}

		public static int SendMessageToWndRevit()
		{
			int wMsg = 7;
			return WindowHelper.SendMessage(WindowHelper._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		private static void NewRevitWnd()
		{
			if (WindowHelper._hWndRevit == null)
			{
				Process currentProcess = Process.GetCurrentProcess();
				IntPtr mainWindowHandle = currentProcess.MainWindowHandle;
				WindowHelper._hWndRevit = new WindowHandle(mainWindowHandle);
			}
		}

		private static WindowHandle _hWndRevit;
	}
}
